<?php 
namespace app\others\controller;
use think\Controller;
use think\Session;
use think\Request;
use think\Db;
//领取免费课程需要的数据
//
class Freecourse extends Controller
{
	public function find_cate($id,$limit=10)
    {
        $c_two = Db::name('category')->where('pid',$id)->field('id')->select();
        $two = '';
        foreach($c_two as $k=>$v){
            $two .= ','. $v['id'];
        }
        
        $ins = substr($two,1);
        // dump($ins);die;
        $c_three = Db::name('category')->where('pid','in',$ins)->field('id')->select();
        foreach($c_three as $k=>$v){
            $two .= ','.$v['id'];
        }
        
        $two .= ','.$id;
        $two = substr($two,1);
        // dump($two);die;
        
        
        return $two;;
    }
	//传入分类id 查免费课程
	public function free($id,$limit=10000)
	{
		$two = $this->find_cate($id,$limit);

		$course = Db::name('course')->alias('c')
            ->field('c.name,c.id,c.description,c.price,p.address')
            ->join('coursepicture p','p.courseid=c.id')
            ->where('c.categoryid','in',$two)
            ->where('c.display','1')
            ->where('p.cover','1')
            ->where('price',0)
            ->limit($limit)
            ->buildsql();
            $res = Db::query($course);
		return json($res);
	}

    //路由
    ////请求免费课程信息接口路由
    //Route::get('/mianfeikecheng/:id/:limit','others/Freecourse/free');
    //请求网址 http://www.yunxiangyishang.xin/mianfeikecheng/ID/LIMIT
    //id取值
    //1:英语能力与考试试听课程
    //2:英语口语试听课程
    //3:出国留学试听课程
    //4:日语试听课程
    //5:韩语试听课程
    //6:考研试听课程
    //7:职场兴趣试听课程
    //8:小语种试听课程
    //9:中小幼教育试听课程
    //10:财会金融试听课程
    //limit:一次请求条数,默认为10
}